package com.ds.dsm.view.config.form.field.group;

import com.ds.common.JDSException;
import com.ds.config.ResultModel;
import com.ds.dsm.view.config.form.field.FieldBaseInputView;
import com.ds.dsm.view.config.form.field.custom.FieldLayoutView;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.FormViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.CustomView;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.dsm.view.field.FieldFormConfig;
import com.ds.esd.tool.ui.enums.Dock;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(path = "/dsm/view/config/form/field/")
public class FieldBaseGroup {
    @CustomAnnotation(pid = true, hidden = true)
    private String esdclass;

    @CustomAnnotation(pid = true, hidden = true)
    private String sourceMethodName;

    @CustomAnnotation(pid = true, hidden = true)
    private String viewInstId;
    @CustomAnnotation(pid = true, hidden = true)
    private String domainId;

    @CustomAnnotation(uid = true, hidden = true)
    private String fieldname;

    public FieldBaseGroup() {
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldCustomView")
    @FormViewAnnotation
    @ModuleAnnotation(imageClass = "xui-icon-dialog", caption = "基础信息", dock = Dock.top, height = "180")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ResultModel<FieldBaseInputView> getFieldBaseView(String sourceClassName, String sourceMethodName, String fieldname, String domainId, String viewInstId) {
        ResultModel<FieldBaseInputView> result = new ResultModel<FieldBaseInputView>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,  viewInstId);
            MethodConfig customMethodAPIBean = classConfig.getSourceConfig().getMethodByName(sourceMethodName);
            CustomView viewBean = customMethodAPIBean.getView();
            FieldFormConfig formInfo = (FieldFormConfig) viewBean.getFieldByName(fieldname);
            formInfo.setDomainId(domainId);
            formInfo.setViewInstId(viewInstId);
            formInfo.setEntityClassName(viewBean.getEntityClassName());
            result.setData(new FieldBaseInputView(formInfo));
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldLayoutView")
    @FormViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-layout", folded = true, caption = "布局", dock = Dock.bottom, height = "150")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ResultModel<FieldLayoutView> getFieldLayoutView(String sourceClassName, String sourceMethodName, String fieldname, String domainId, String viewInstId) {
        ResultModel<FieldLayoutView> result = new ResultModel<FieldLayoutView>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig customMethodAPIBean = classConfig.getSourceConfig().getMethodByName(sourceMethodName);
            CustomView viewBean = customMethodAPIBean.getView();
            FieldFormConfig formInfo = (FieldFormConfig) viewBean.getFieldByName(fieldname);
            formInfo.setDomainId(domainId);
            formInfo.setViewInstId(viewInstId);

            formInfo.setEntityClassName(viewBean.getEntityClassName());
            result.setData(new FieldLayoutView(formInfo));
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }

    public String getDomainId() {
        return domainId;
    }

    public void setDomainId(String domainId) {
        this.domainId = domainId;
    }

    public String getSourceMethodName() {
        return sourceMethodName;
    }

    public void setSourceMethodName(String sourceMethodName) {
        this.sourceMethodName = sourceMethodName;
    }

    public String getFieldname() {
        return fieldname;
    }

    public void setFieldname(String fieldname) {
        this.fieldname = fieldname;
    }

    public String getEsdclass() {
        return esdclass;
    }

    public void setEsdclass(String esdclass) {
        this.esdclass = esdclass;
    }

    public String getViewInstId() {
        return viewInstId;
    }

    public void setViewInstId(String viewInstId) {
        this.viewInstId = viewInstId;
    }
}
